package com.wshl.aijiuunion.utils;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.wshl.aijiuunion.R;


/**
 * Created by Administrator on 2018/9/18.
 * 自定义对话框
 */

public class DialogUtil {


    /**
     * @param context
     * @param content   对话框内容
     * @param cancancel 触摸屏幕是否dialog消失 true 消失 false 不消失
     *                  isShowTitle 是否显示标题
     */
    public static void showCustomdialog(Context context, String content, boolean cancancel, boolean isShowTitle, String title, String cancel, String confirm) {
        final Dialog dialog = new Dialog(context, R.style.myDialog);
        View view1 = View.inflate(context, R.layout.dialog, null);
        TextView tvContent = view1.findViewById(R.id.tv_content);

        //对话框内容
        tvContent.setText(content);

        TextView tvTitle = view1.findViewById(R.id.tv_title);//标题
        TextView tvCancel = view1.findViewById(R.id.tv_exit);//取消
        TextView tvConfirm = view1.findViewById(R.id.tv_confirm);//确定

        if (!TextUtils.isEmpty(title)) {
            tvTitle.setText(title);
        }
        if (!TextUtils.isEmpty(cancel)) {
            tvCancel.setText(cancel);
        }
        if (!TextUtils.isEmpty(confirm)) {
            tvConfirm.setText(confirm);
        }

        //是否显示标题
        if (isShowTitle) {
            tvTitle.setVisibility(View.VISIBLE);
        } else {
            tvTitle.setVisibility(View.GONE);
        }

        //取消
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dialog != null) {
                    dialog.dismiss();
                }

                if (onDialogConfirm != null) {
                    onDialogConfirm.setCancel();
                }
            }
        });

        //确定
        tvConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dialog != null) {
                    dialog.dismiss();
                }

                if (onDialogConfirm != null) {
                    onDialogConfirm.setConfirm();
                }
            }
        });

        dialog.setContentView(view1);
        dialog.setCancelable(cancancel);
        dialog.show();
    }


    /**
     * @param context
     * @param content   对话框内容
     * @param isInput   是都显示输入框
     * @param cancancel 触摸屏幕是否dialog消失 true 消失 false 不消失
     *                  isShowTitle 是否显示标题
     */
    public static void showNormaldialog(Context context, String content, final boolean isInput, boolean cancancel) {
        final Dialog dialog = new Dialog(context, R.style.myDialog);
        // final Dialog dialog = new Dialog(context);
        View view1 = View.inflate(context, R.layout.dialog, null);
        TextView tvContent = view1.findViewById(R.id.tv_content);
        final EditText etContent = view1.findViewById(R.id.et_content);
        TextView tvTitle = view1.findViewById(R.id.tv_title);//标题
        TextView tvCancel = view1.findViewById(R.id.tv_exit);//取消
        TextView tvConfirm = view1.findViewById(R.id.tv_confirm);//确定

        if (!TextUtils.isEmpty(content)) {


            if (isInput) {
                etContent.setVisibility(View.VISIBLE);
                tvContent.setVisibility(View.GONE);
                etContent.setHint(content);
            }else{
                etContent.setVisibility(View.GONE);
                tvContent.setVisibility(View.VISIBLE);
                tvContent.setText(content);
            }
        }

        //取消
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dialog != null) {
                    dialog.dismiss();
                }

                if (onDialogConfirm != null) {
                    onDialogConfirm.setCancel();
                }
            }
        });

        //确定
        tvConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isInput) {

                    String inputcontent = etContent.getText().toString().trim();

                    if (TextUtils.isEmpty(inputcontent)) {

                        ToastUtils.showShort("请输入有效内容");
                        return;
                    }

                    if (onDialogConfirm != null) {
                        onDialogConfirm.getInput(inputcontent);

                    }

                } else {


                    if (onDialogConfirm != null) {

                        onDialogConfirm.setConfirm();

                    }

                }


                if (dialog != null) {
                    dialog.dismiss();

                }


            }
        });

        dialog.setContentView(view1);
        dialog.setCancelable(cancancel);
        dialog.show();
    }


    public static OnDialogConfirm onDialogConfirm;

    public static void setOndialgConfirm(OnDialogConfirm ondialgConfirm) {

        onDialogConfirm = ondialgConfirm;

    }

    public interface OnDialogConfirm {

        void setConfirm();

        void setCancel();

        void getInput(String input);
    }





    /**
     * @param context
     */
    public static void showBottomDialog(Context context, final String title1, final String title2, final String title3, String color1, String color2, String color3) {
        final Dialog dialog = new Dialog(context, R.style.bottom_dialog);
        View view1 = View.inflate(context, R.layout.dialog_bottom, null);

        TextView tvCancel = view1.findViewById(R.id.tv_bottom_cancel);//取消
        TextView tvTitle1 = view1.findViewById(R.id.tv_title1);//标题1
        TextView tvTitle2 = view1.findViewById(R.id.tv_title2);//标题2
        TextView tvTitle3 = view1.findViewById(R.id.tv_tile3);//标题3

        tvTitle1.setText(title1);
        tvTitle2.setText(title2);
        tvTitle1.setTextColor(Color.parseColor(color1));
        tvTitle2.setTextColor(Color.parseColor(color2));

        if (!TextUtils.isEmpty(title3)) {
            tvTitle3.setVisibility(View.VISIBLE);
            tvTitle3.setText(title3);
            tvTitle3.setTextColor(Color.parseColor(color3));
        }

        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        tvTitle1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                dialog.dismiss();

                if (onBottomDilogClick != null) {
                    onBottomDilogClick.setTitle1(title1);
                }

            }
        });

        tvTitle2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                dialog.dismiss();
                if (onBottomDilogClick != null) {
                    onBottomDilogClick.setTitle2(title2);
                }
            }
        });

        tvTitle3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                dialog.dismiss();
                if (onBottomDilogClick != null) {
                    onBottomDilogClick.setTitle3(title3);
                }
            }
        });

        dialog.setContentView(view1);
        dialog.setCancelable(true);
        Window dialogWindow = dialog.getWindow();
        dialogWindow.setGravity(Gravity.BOTTOM);
        //设置对话框的宽度占满屏幕,高度自适应
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
        lp.y = ViewGroup.LayoutParams.WRAP_CONTENT;
        //设置Dialog距离底部的距离
        dialogWindow.setAttributes(lp);
        dialog.show();
    }


    public static OnBottomDilogClick onBottomDilogClick;

    public static void setOnBottomDilogClick(OnBottomDilogClick bottomDilogClick) {

        onBottomDilogClick = bottomDilogClick;

    }

    public interface OnBottomDilogClick {

        void setTitle1(String title);

        void setTitle2(String title);

        void setTitle3(String title);
    }

}
